/**
 * Project Name: socket-test
 * Package Name: com.meatball
 * File Name: MsgController
 * Date: 2021/1/26 23:33
 * User: 張翔宇
 * Copyright (c) 2021, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;

/**   
 * 
 * @title: MsgController.java
 * @author: 張翔宇
 * @date: 2021/1/26 23:34 
 * @version: V3.0   
 */
@RestController
@RequestMapping("/msg")
@AllArgsConstructor
public class MsgController {
    private final ConcurrentHashMap<String, WebSocketSender> senderMap;

    @GetMapping("/send")
    public String sendMessage(String id, String data) {
        WebSocketSender sender = senderMap.get(id);
        if (sender != null) {
            sender.sendData(data);
            return String.format("消息 '%s' 已经发送给：%s.", data, id);
        } else {
            return String.format("用户 '%s' 不在线。", id);
        }
    }
}
